package com.shujia.dao.impl;

import com.shujia.dao.DeptDao;
import com.shujia.pojo.Dept;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 需求：
 *      从MySQL中查询部门表，再将部门表中编号为30的数据返回到页面中
 */

@Repository
public class DeptDaoImpl implements DeptDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    Dept dept;

    @Override
    public String getDept() {
        String sql ="select * from bigdata19.dept where deptno='30'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : maps) {
            dept.setDeptno((Integer)map.get("DEPTNO"));
            dept.setDname((String)map.get("DNAME"));
            dept.setLoc((String)map.get("LOC"));
        }
        return dept.toString();
    }





    @Override
    public List<Dept> getAllCusBaseInfo() {
        //查询部门信息
        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from dept");
        List<Dept> cusBaseInfoList=new ArrayList<>();
        for (Map<String,Object> map:list ) {

            Dept cusBaseInfo=new Dept();
            //部门id
            cusBaseInfo.setDeptno((Integer)map.get("deptno"));
            cusBaseInfo.setDname(map.get("dname").toString());
            cusBaseInfo.setLoc(map.get("loc").toString());
            cusBaseInfoList.add(cusBaseInfo);
        }
        return cusBaseInfoList;
    }

    @Override
    public Dept saveDeptInfo(Dept dept) {
//        dept.setDeptno(UUID.randomUUID().toString());
        System.out.println(dept);
        String sql="insert into dept (deptno,dname,loc) values(?,?,?)";
        jdbcTemplate.update(sql,
                new Object[]{
                dept.getDeptno(),
                dept.getDname(),
                dept.getLoc(),},new int[]{
                        Types.INTEGER,Types.VARCHAR,Types.VARCHAR});
        //获取信息
        return getDeptByNo(dept.getDeptno());
    }
    private static final Logger log = LoggerFactory.getLogger(DeptDaoImpl.class);

    @Override
    public Dept getDeptByNo(Integer deptNo) {

//        log.error("getDeptByNo错误发生....");
        log.info("getDeptByNo正在执行....");
        Map<String,Object> map=jdbcTemplate.queryForMap("select * from dept where deptno = ?",deptNo);
        Dept cusBaseInfo=new Dept();

        cusBaseInfo.setDeptno((Integer)map.get("deptno"));

        cusBaseInfo.setDname(map.get("dname").toString());

        cusBaseInfo.setLoc(map.get("loc").toString());
        return cusBaseInfo;

    }

    @Override
    public String updateDept(Dept dept) {
        String sql="update  dept set loc = ? where DEPTNO = ?";
        jdbcTemplate.update(sql,
                new Object[]{
                        dept.getLoc(),
                        dept.getDeptno()
                        },new int[]{
                        Types.VARCHAR,
                        Types.INTEGER});
        return getDeptByNo(dept.getDeptno()).toString();
    }
}
